package pl.edu.pk.psb.controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class UploadPlikowController {
	
	@Autowired
	private ServletContext context;

	@Value("${images.dir}")
	private String sciezkaDoObrazow;
	
	@RequestMapping(value = "upload", method = RequestMethod.POST)
	public @ResponseBody
	String wgrajPlik(MultipartFile plik, HttpServletRequest request) {
		File file = new File(sciezkaDoObrazow);
		file.mkdirs();
		File f = new File(file, plik.getOriginalFilename());
		try {
			plik.transferTo(f);
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return plik.getOriginalFilename();
	}

	@RequestMapping(value = "uploadPlikow")
	public ModelAndView przekieruj() {
		return new ModelAndView("/podstrony/uploadObrazka");
	}

}
